#include "Host.h"

Network::Host::Host()
{
}

Network::Host::~Host()
{
}

unsigned int	Network::Host::Init(unsigned short int port, const std::string& address)
{
	unsigned int	error;

	if ((error = this->CreateSocket()) != 0)
		return (error);
	return (this->BindSocket(port, address));
}

unsigned int	Network::Host::Listen(unsigned int queueLenght)
{
	if (listen(this->_socket, queueLenght) != 0)
		return (1);
	return (0);
}

unsigned int	Network::Host::Accept(Connection** incomingConnection)
{
#ifdef _WIN32
	SOCKET		socket;
	SOCKADDR_IN	socketInfos;
#else
	unsigned int	socket;
	sockaddr_in		socketInfos;
#endif
	int				socketInfosLenght = sizeof(socketInfos);

#ifdef _WIN32
	socket = accept(this->_socket, (SOCKADDR*)&socketInfos, &socketInfosLenght);
#else
	socket = accept(this->_socket, (sockaddr*)&socketInfos, &socketInfosLenght);
#endif
	if (socket < 0)
		return (1);
	*incomingConnection = new Connection(inet_ntoa(socketInfos.sin_addr), ntohs(socketInfos.sin_port), socket, socketInfos);
	return (0);
}